package com.mcr.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcr.reggie.dao.EmployeeMapper;
import com.mcr.reggie.entity.Employee;
import com.mcr.reggie.service.EmployeeService;
import com.mcr.reggie.utils.BaseContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Value("${employee.defaultPassworld}")
    private String defaultPass;
    @Value("${employee.rootId}")
    private String rootId;
    @Override
    public Employee loginVerify(Employee employee) {
        //1、将页面提交的密码password进行md5加密处理
        String password=employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());
        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername())
                    .eq(Employee::getPassword,password);
        Employee emp=employeeMapper.selectOne(queryWrapper);
        return emp;
    }
    @Override
    public void saveEmp(Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex(defaultPass.getBytes()));
        employeeMapper.insert(employee);
    }
    @Override
    public Page<Employee> getPage(int page, int pageSize, String name) {
        Page<Employee> pageInfo=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name)
                    .orderByDesc(Employee::getCreateTime);
        employeeMapper.selectPage(pageInfo,queryWrapper);
        return pageInfo;
    }
    @Override
    public Boolean updateEmp(Employee employee) {
        Long id = BaseContext.getEmp();
        if(id==Long.valueOf(rootId)){
            employee.setStatus(1);
            employeeMapper.updateById(employee);
            return true;
        }
        return false;
    }
}
